package com.handong.handserialprogram.module.createmission.recordtask;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.model.entities.PersonEntity;

import java.util.ArrayList;

/**
 * Created by userKey on 2017/8/10.
 */

public class CarAdapter extends RecyclerView.Adapter<CarAdapter.CarHolder> {

    private ArrayList<PersonEntity> mCarList;
    private CarFragment mCarFragment;
    private CarHolder mLostChecked;
    private boolean is_Car;

    public CarAdapter(ArrayList<PersonEntity> list, CarFragment fragment,boolean isCar) {
        mCarList = list;
        mCarFragment = fragment;
        is_Car=isCar;
    }

    @Override
    public CarHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_car_item, parent, false);
        return new CarHolder(rootView);
    }

    @Override
    public void onBindViewHolder(CarHolder holder, int position) {
        if (is_Car){
            PersonEntity entity = mCarList.get(position);
            holder.entity = entity;
            holder.id.setText("车牌: " + entity.getName());
            holder.bg_pic.setImageResource(R.drawable.icon_police_bus);
            if (entity.isChecked()) {
                holder.status.setImageResource(R.drawable.person_status_checked);
                mLostChecked = holder;
            } else {
                holder.status.setImageResource(R.drawable.person_status_unchecked);
            }
        }else {
            PersonEntity entity = mCarList.get(position);
            holder.entity = entity;
            holder.id.setText("路线: " + entity.getName());
            holder.bg_pic.setImageResource(R.drawable.icon_car_line);
            if (entity.isChecked()) {
                holder.status.setImageResource(R.drawable.person_status_checked);
                mLostChecked = holder;
            } else {
                holder.status.setImageResource(R.drawable.person_status_unchecked);
            }
        }

    }

    @Override
    public int getItemCount() {
        return mCarList == null ? 0 : mCarList.size();
    }

    public void onDestroy() {
        mCarFragment = null;
        mLostChecked = null;
    }

    class CarHolder extends RecyclerView.ViewHolder {

        private TextView id;
        private ImageView status,bg_pic;
        private PersonEntity entity;

        public CarHolder(View itemView) {
            super(itemView);
            bg_pic=itemView.findViewById(R.id.bg_pic);
            id = (TextView) itemView.findViewById(R.id.itemCarId);
            status = (ImageView) itemView.findViewById(R.id.chooseCarStatus);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!entity.isChecked()) {
                        if (mLostChecked != null) {
                            mLostChecked.entity.setChecked(false);
                            mLostChecked.status.setImageResource(R.drawable.person_status_unchecked);
                        }

                        entity.setChecked(true);
                        status.setImageResource(R.drawable.person_status_checked);
                        mCarFragment.setChooseCar(entity.getId(), entity.getName());
                        mLostChecked = CarHolder.this;
                    }
                }
            });
        }
    }
}
